/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RunableClass;
import net.ipaddress.*;
import STOCK.*;
import java.awt.*;
import file.fileread.*;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
import javax.swing.JOptionPane;
import net.ipaddress.*;
/**
 *
 * @author Anatomy
 */
public class IndexRunnable implements Runnable {
        static  int              count          =   1;
        static  StringBuffer     content        =   new StringBuffer("");
                int              count1         =   0;
public  static  int              avg            =   0;
                int              a              =   0;    
public  static  String           datetime;

                String           index[]        =   {"SET"};
                int              count4         =   0;
        static  int              count5         =   1;
        static  StringBuffer     content6       =   new StringBuffer("");
   
   // public run(){
      
        
            
           //  }

    @Override
    public void run() {
          for(;;){ 
              /////////////////////////////
          
         
        ///////////////////สร้างวันและเวลา//////////////////////
        /**/ Calendar now = Calendar.getInstance();     /**/
        /**/    int h = now.get(Calendar.HOUR_OF_DAY);  /**/
        /**/    int m = now.get(Calendar.MINUTE);       /**/
        /**/    int s = now.get(Calendar.SECOND);       /**/
        /**/    int i = now.get(Calendar.MILLISECOND);  /**/
        /**/   datetime =(h + ":" + m + ":" + s);       /**/
        ///////////////////////////////////////////////////
        
        
     //////////////////////////////////สร้างการดึงค่าจาก URL/////////////////////////////////////////////////////////////////////////////**/
     /**/         try {
           content = new StringBuffer("");int count2=1;
                
         String buffer;
        String url = "http://www.settrade.com/C13_MarketSummary.jsp",                                                         /**/                                                      
     /**/          proxy = "proxy.up.ac.th",                                                                                        /**/
     /**/          port = "8080",                                                                                                   /**/
     /**/          username = "up\\51286885",                                                                                       /**/
     /**/          password = "7489250721";                                                                                         /**/
     /**/                                                                                                                           /**/
     /**/     // URL autoIP = new URL("http://checkip.dyndns.org:8245/");                                                          /**/
     /**/     //  System.out.println("Get URL");       
        /**/  
     /**/     STOCK.IPHostName.setText( new  IPAdress().getMyPublicIP());                                                                                                                    /**/
     /**/     URL server = new URL(url);                                                                                            /**/
     /**/                                                                                                                           /**/
     /**/       Authenticator.setDefault(new authenticator(                                                                         /**/
     /**/                   username,password));                                                                                    /**/
     /**/       Properties systemProperties = System.getProperties();                                                               /**/
     /**/       systemProperties.setProperty("http.proxyHost",proxy);                                                               /**/
     /**/       systemProperties.setProperty("http.proxyPort",port);                                                                /**/
     /**/       HttpURLConnection connection = (HttpURLConnection)server.openConnection();                                          /**/
     /**/      connection.connect();                                                                                                /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                           /**/
     /**/       try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620"))) {        /**/
     /**/                                                                                                                           /**/
     /**/                                                  /**/                                                                     /**/
     /**/                content.append(datetime).append("\n");                                                                     /**/
     /**/           while((buffer = in.readLine()) != null) {                                                                     /**/  
     /**/                  count2++;                                                                                              /**/  
     /**/              // avg=(count2/count)*100;                                                                                 /**/  
     /**/                                                                                                                         /**/  
     /**/                  content.append(count2).append("-->").append(buffer).append("\n");                                      /**/
     /**/                                                                                                                         /**/      
      /**/            }                                                                                                           /**/  
      /**/      }                                                                                                                 /**/  
     /**/             } catch (IOException io){
            System.err.println("Couldn't create help URL: " + io);
        javax.swing.JOptionPane.showMessageDialog(null,"No Internet Connection.\nPlease connect internet.\n"
                + "or Disable Firewall Before open program","Connect Error",JOptionPane.ERROR_MESSAGE);
     System.exit(0);

    }         
     
     /**/        
     /**///////////////////////////อ่านค่าหลังจากที่ดึง//////////////////////////////////////////////////////////////////////////////////**/ 
              
            try {
                           new Readindexstock().readFromFile(content.toString(),index[count1]);
            } catch (SQLException ex) {
            }
                
                
              if(Readindexstock.sym[3].charAt(0)=='-'){
                  STOCK.txtchg.setForeground(Color.red);
                  STOCK.txtchg.setText(Readindexstock.sym[3]);
                   STOCK.Now.setForeground(Color.red);
                   STOCK.Now.setText(Readindexstock.sym[1]);
                    }else{
                  STOCK.txtchg.setForeground(Color.GREEN);
                  STOCK.txtchg.setText(Readindexstock.sym[3]);   
                   STOCK.Now.setForeground(Color.GREEN);
                   STOCK.Now.setText(Readindexstock.sym[1]);
                 }
                 if(Readindexstock.sym[4].charAt(0)=='-'){
                     STOCK.txtpchg.setForeground(Color.red);
                     STOCK.txtpchg.setText(Readindexstock.sym[4]);
                 }else{
                      STOCK.txtpchg.setForeground(Color.GREEN);
                      STOCK.txtpchg.setText(Readindexstock.sym[4]);
                 }
                  STOCK.MkV.setText(Readindexstock.sym[8]);
                  STOCK.hg.setText(Readindexstock.sym[5]);
                  STOCK.lw.setText(Readindexstock.sym[6]);
                  STOCK.txtvol.setText(Readindexstock.sym[7]);
                   STOCK.txttime.setText(datetime);
                 
                  // JEditorPane je=new JEditorPane("text/html",Readindexstock.status);
                   
                   
                   if(Readindexstock.status==0){
                        STOCK.txtstatus.setForeground(Color.red);
                        STOCK.txtstatus.setText("CLOSE");
                      
                      //       GETThread.t2.suspend();
                        //      GETThread.t3.suspend();
                         //     GETThread.t4.suspend();
                   }else if(Readindexstock.status==1){
                       STOCK.txtstatus.setForeground(Color.GREEN);
                       STOCK.txtstatus.setText("OPEN");
                      //  GETThread.t2.resume();
                      //        GETThread.t3.resume();
                      //        GETThread.t4.resume();
                   }else if(Readindexstock.status==2){
                       STOCK.txtstatus.setForeground(Color.white);
                       STOCK.txtstatus.setText("Intermission");
                   }else if(Readindexstock.status==3){
                       STOCK.txtstatus.setForeground(Color.white);
                       STOCK.txtstatus.setText("Call");
                   }else if(Readindexstock.status==4){
                       STOCK.txtstatus.setForeground(Color.white);
                       STOCK.txtstatus.setText("Run-off");
                   }
                  STOCK.index.setText(index[count1]); 
                ++ count1;
                 if(count1==1){
                    
                    count1=0;
                 }
        
        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
        }
      // / ++a;
     //   count1++;
        }
     
    }
}
    

